/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bff.mpd.musicjukebox.ui.components;

import com.bff.bjj.ui.component.MusicListCellRenderer;
import com.bff.mpd.musicjukebox.lib.ColorProperties;
import java.awt.Component;
import java.util.HashMap;
import javax.swing.JList;
import org.bff.javampd.objects.MPDAlbum;

/**
 *
 * @author bfindeisen
 */
public class AlbumListCellRenderer extends MusicListCellRenderer {

    private static HashMap<MPDAlbum, AlbumCellPanel> albumPanels;

    public AlbumListCellRenderer() {
        super();
        albumPanels = new HashMap<MPDAlbum, AlbumCellPanel>();
    }

    /**
     * must clear for memory problems
     */
    public void clearPanels() {
        albumPanels = new HashMap<MPDAlbum, AlbumCellPanel>();
    }

    @Override
    public Component getListCellRendererComponent(JList list,
            Object value,
            int index,
            boolean isSelected,
            boolean isFocused) {

        AlbumCellPanel panel = null;

        if (value instanceof MPDAlbum) {
            MPDAlbum album = (MPDAlbum) value;
            panel = albumPanels.get(album);
            if (panel == null) {
                panel = new AlbumCellPanel(album.getArtist(), album, list);
                albumPanels.put(album, panel);
            }
        } else {
            return super.getListCellRendererComponent(list, value, index, isSelected, isFocused);
        }
        if (panel != null) {
            if ((index % 2) == 0) {
                panel.setEven();
            } else {
                panel.setOdd();
            }

            if (!isSelected) {
                panel.setForeground(ColorProperties.LIST_FOREGROUND);
                panel.setNotSelected();
            } else {
                panel.setSelected();
            }

            if (panel.getPreferredSize().getHeight() > 1) {
                list.setFixedCellHeight((int) panel.getPreferredSize().getHeight());
            }

        }

        setColors(list, index, isSelected);

        if (panel != null) {
            return panel;
        } else {
            setText(value.toString());
            return this;
        }
    }
}
